/**
 * 选择会计期间界面
 * 
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
PCL.define("PSI.SLN0002.GLPeriod.SelectPeriodForm", {
  extend: "PSI.AFX.Form.EditForm",

  config: {
    company: null,
  },

  /**
   * 初始化组件
   * 
   * @override
   */
  initComponent() {
    const me = this;

    const buttons = [{
      text: "确定",
      ...PSI.Const.BTN_STYLE,
      formBind: true,
      iconCls: "PSI-button-ok",
      handler() {
        me._onOK();
      },
      scope: me
    }, {
      text: "取消",
      ...PSI.Const.BTN_STYLE,
      handler() {
        me.close();
      },
      scope: me
    }];

    const logoHtml = me.genLogoHtml(null, "初始化会计期间");

    const width1 = 600;

    PCL.apply(me, {
      header: {
        title: me.formatTitle(PSI.Const.PROD_NAME),
        height: 40
      },
      width: 650,
      height: 300,
      layout: "border",
      items: [{
        region: "north",
        height: 70,
        border: 0,
        html: logoHtml
      }, {
        region: "center",
        border: 0,
        xtype: "form",
        layout: {
          type: "table",
          columns: 1,
          tableAttrs: PSI.Const.TABLE_LAYOUT,
        },
        height: "100%",
        bodyPadding: 5,
        defaultType: 'textfield',
        fieldDefaults: {
          labelWidth: 90,
          labelAlign: "right",
          labelSeparator: "",
          msgTarget: 'side',
          width: width1,
        },
        items: [{
          xtype: "displayfield",
          fieldLabel: "组织机构",
          value: me.toFieldNoteText(me.getCompany().get("name")),
        }, {
          id: me.buildId(me, "editQueryPeriod"),
          beforeLabelTextTpl: PSI.Const.REQUIRED,
          fieldLabel: "建账会计期间",
          labelWidth: 120,
          margin: 0,
          xtype: "fieldcontainer",
          layout: "hbox",
          items: [
            {
              xtype: "numberfield",
              hideTrigger: true,
              allowDecimals: false,
              width: 40,
              id: me.buildId(me, "editYear"),
              value: (new Date()).getFullYear(),
            }, { xtype: "displayfield", value: "年", width: 15, }, {
              xtype: "combobox",
              width: 70,
              store: PCL.create("PCL.data.ArrayStore", {
                fields: ["id", "text"],
                data: [[1, "一月"], [2, "二月"],
                [3, "三月"], [4, "四月"],
                [5, "五月"], [6, "六月"],
                [7, "七月"], [8, "八月"],
                [9, "九月"], [10, "十月"],
                [11, "十一月"], [12, "十二月"]]
              }),
              valueField: "id",
              displayFIeld: "text",
              queryMode: "local",
              editable: false,
              id: me.buildId(me, "editMonth"),
            }, {
              xtype: "displayfield",
              value: "&nbsp;(录入凭证从建账会计期间的下一个月开始)"
            }
          ],
        }, {
          xtype: "displayfield",
          value: "<h4 style='margin-left:30px;color:gray'>请仔细确认建账会计期间，初始化后就不能再更改</h4>"
        }],
        buttons,
      }],
      listeners: {
        show: {
          fn: me._onWndShow,
          scope: me
        },
      },
    });

    me.callParent(arguments);

    me.editYear = PCL.getCmp(me.buildId(me, "editYear"));
    me.editMonth = PCL.getCmp(me.buildId(me, "editMonth"));

  },

  /**
   * @private
   */
  _onOK() {
    const me = this;

    const year = me.editYear.getValue();
    const month = me.editMonth.getValue();
    if (!year) {
      me.showInfo("没有输入会计期间的年份", () => {
        me.editYear.focus();
      });
      return;
    }
    if (!month) {
      me.showInfo("没有输入会计期间的月份", () => {
        me.editMonth.focus();
      });
      return;
    }
    let p = '';
    if (month < 10) {
      p = `${year}-0${month}`;
    } else {
      p = `${year}-${month}`;
    }

    const info = `建账会计期间：${p} (完成初始化后就不能再修改了)
                  </p>
                  请确认是否执行 ${me.htInfo("初始化会计期间")} 操作?`;

    const funcConfirm = () => {
      const el = PCL.getBody();
      el.mask("正在操作中...");
      const r = {
        url: me.URL("SLN0002/GLPeriod/initPeriod"),
        params: {
          companyId: me.getCompany().get("id"),
          year,
          month,
        },
        callback(options, success, response) {
          el.unmask();

          if (success) {
            const data = me.decodeJSON(response.responseText);
            if (data.success) {
              me.tip("成功完成初始化操作", true);
              const parentForm = me.getParentForm();
              if (parentForm) {
                parentForm.refreshCompanyGrid.apply(parentForm);
              }
              me.close();
            } else {
              me.showInfo(data.msg);
            }
          } else {
            me.showInfo("网络错误");
          }
        }
      };
      me.ajax(r);
    };

    me.confirm(info, funcConfirm);
  },

  /**
   * @private
   */
  _onWndShow() {
    const me = this;

    me.editYear.focus();
  },
});
